//
//  HMTabbarItem.swift
//  
//
//  Created by 饶鹏伟 on 16/4/15.
//
//

import UIKit
//底部工具条的每个按钮
class HMTabbarItem: UITabBarItem {
    
    //一旦外界修改 badgeValue  就要给当前的tabbarbutton badgeValue修改背景视图

    override var badgeValue:String?{
        
        didSet{
            
            //获取按钮的所在的控制器？
            let target = self.valueForKey("_target") as! HMMainViewController
            
            //print(target)
            //print(badgeValue)
            
            for subView in target.tabBar.subviews {
                if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    for v in subView.subviews {
                        if v.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            for bgView in v.subviews {
                                if bgView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                            
                                    print(bgView)
                                    //通过KVC 来设置成员变量 通过调试台查看 成员变量
                                    bgView.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
